package com.xinchuang.mapper;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xinchuang.domain.pojo.TestNoticeTimeTable;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * Mapper 接口
 * </p>
 *
 * @author zx
 * @since 2023-07-04
 */
@Mapper
public interface TestNoticeTimeTableMapper extends BaseMapper<TestNoticeTimeTable> {

    @Select(value = {
            "<script>",
            "SELECT",
            "tnmt.id,",
            "tmst.mold_id,",
            "tmst.mold_name,",
            "tmst.try_out_times,",
            "tmst.tongman_name,",
            "tnmt.project_manager,",
            "tnmt.`status`,",
            "tnmt.note_name,",
            "tmst.project_name,",
            "tnmt.edition_notice,",
            "tmp.project_name AS projectName,",
            "tmp.tongman_name AS tongmanName,",
            "tnmt.report_id AS reportId",
            "FROM",
            "test_notice_main_tabale tnmt",
            "JOIN test_model_sub_table tmst ON tnmt.parent_plan_sub_id = tmst.plan_id",
            "JOIN test_model_param tmp ON tmst.mold_id = tmp.mold_id",
            "WHERE 1 = 1",
            "<if test='list == null'>",
            "AND tnmt.`status` NOT IN (0,5,6,7)",
            "</if>",
            "<if test='list != null'>",
            "AND tnmt.`status` IN",
            "<foreach collection='list' item='item' open='(' separator=',' close=')'>",
            "#{item}",
            "</foreach>",
            "</if>",
            "<if test='username != null and username != &apos;&apos;'>",
            "AND tnmt.project_manager LIKE '%${username}%'",
            "</if>",
            "<if test='noteId != null and noteId != &apos;&apos;'>",
            "AND tnmt.id LIKE '%${noteId}%'",
            "</if>",
            "<if test='moldId != null and moldId != &apos;&apos;'>",
            "AND tmst.mold_id LIKE '%${moldId}%'",
            "</if>",
            "<if test='moldName != null and moldName != &apos;&apos;'>",
            "AND (tmst.mold_name LIKE '%${moldName}%' OR tmp.mold_name LIKE '%${moldName}%')",
            "</if>",
            "<if test='noteName != null and noteName != &apos;&apos;'>",
            "AND tnmt.note_name LIKE '%${noteName}%'",
            "</if>",
            "<if test='deviceId != null and deviceId != &apos;&apos;'>",
            "AND tnmt.try_out_device_id LIKE '%${deviceId}%'",
            "</if>",
            "<if test='T0TimeStart != null and T0TimeStart != &apos;&apos;'>",
          /*  "AND tmst.try_out_times = 0 AND tmst.create_time BETWEEN '${T0TimeStart}' AND '${T0TimeEnd}'",*/
            "AND tmst.planned_time BETWEEN '${T0TimeStart}' AND '${T0TimeEnd}'",
            "</if>",
            "<if test='tongName != null'>",
            "AND tmst.tongman_name IN ",
            "<foreach collection='tongName' item='item' open='(' separator=',' close=')'>",
            "#{item}",
            "</foreach>",
            "</if>",
            "<if test='projectName != null and projectName != &apos;&apos;'>",
            "AND tnmt.project_name LIKE '%${projectName}%'",
            "</if>",
            "</script>"
    })
    IPage<Map<String, Object>> getTryOutRecordPage(Page page,
                                                   @Param("username") String username,
                                                   @Param("noteId") String noteId,
                                                   @Param("moldId") String moldId,
                                                   @Param("moldName") String moldName,
                                                   @Param("noteName") String noteName,
                                                   @Param("deviceId") String deviceId,
                                                   @Param("T0TimeStart") String T0TimeStart,
                                                   @Param("T0TimeEnd") String T0TimeEnd,
                                                   @Param("list") List<String> status,
                                                   @Param("tongName") List<String> tongName,
                                                   @Param("projectName") String projectName);

    @Select(value = {
            "SELECT mst.try_out_times,nmt.try_out_reason_id,",
            "DATE_FORMAT(mst.planned_time, '%Y-%m-%d') AS planned_time,",
            "DATE_FORMAT(mst.practical_time, '%Y-%m-%d') AS practical_time,",
            "nmt.sub_target",
            "FROM test_model_sub_table mst",
            "RIGHT JOIN test_notice_main_tabale nmt ON mst.plan_id = nmt.parent_plan_sub_id",
            "WHERE mst.mold_id = #{moldId} AND mst.try_out_times = #{tryOutTimes}",
            "LIMIT 1"
    })
    @Results(value = {
            @Result(property = "planned_time", column = "planned_time", javaType = String.class),
            @Result(property = "practical_time", column = "practical_time", javaType = String.class)
    })
    Map<String, Object> getModelStage(@Param("moldId") String moldId, @Param("tryOutTimes") String tryOutTimes);

    @Select(value = "SELECT SUM(plan) AS plan,SUM(actual) AS actual FROM test_record_resume_table WHERE parent_source_id = #{parentSourceId}")
    @Results(value = {
            @Result(column = "plan", property = "plan", javaType = Integer.class),
            @Result(column = "actual", property = "actual", javaType = Integer.class),
    })
    Map<String, Integer> getResumeAll(@Param("parentSourceId") String parentSourceId);

}
